package com.ysjl.badminton.utils;


import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;

/**
 * @fileName HttpUtils.java
 * @description
 */
@Slf4j
public class HttpUtils {

    /**
     * HTTPS GET请求
     * @param url
     * @return
     */
    public static String getHttps(String hostname, String url) {
        SSLHttpClient sslHttpClient = new SSLHttpClient();
        HttpClient httpClient = null;
        String result = null;
        try {
            httpClient = sslHttpClient.registerSSL(hostname, "TLS", 443, "https");
        } catch (Exception e) {
            log.error("构建HTTPS请求异常！", e);
        }
        HttpGet get = new HttpGet(url);
        try {
            HttpResponse response = httpClient.execute(get);
            if (response != null) {
                if (200 == response.getStatusLine().getStatusCode()) {
                    HttpEntity httpEntity = response.getEntity();
                    if (httpEntity != null) {
                        result = EntityUtils.toString(httpEntity, "UTF-8");
                    }
                }
                else {
                    log.error("Https Get 请求失败！");
                }
            }

        } catch (Exception e) {
            log.error("Https Get 请求异常！", e);
        }
        return result;
    }

    /**
     * HTTPS POST JSON请求
     * @param url
     * @param jsonParam
     * @return
     */
    public static String httpsPostWithJson(String hostname, String url, String jsonParam) {
        return HttpUtils.httpsPostWithJson(hostname, url, jsonParam, null);
    }

    /**
     * HTTPS POST JSON WITH HEADER
     * @param url
     * @param jsonParam
     * @param header
     * @return
     */
    public static String httpsPostWithJson(String hostname, String url, String jsonParam, Map<String, String> header) {
        SSLHttpClient sslHttpClient = new SSLHttpClient();
        DefaultHttpClient httpClient = null;
        try {
            httpClient = sslHttpClient.registerSSL(hostname, "TLS", 443, "https");
        } catch (Exception e) {
            log.error("构建HTTPS请求异常！", e);
        }
        HttpPost method = new HttpPost(url);
        String body = null;


        // 处理请求
        if (method != null & jsonParam != null
                && !"".equals(jsonParam.trim())) {
            try {

                method.addHeader("Content-type",
                        "application/json; charset=utf-8");
                method.setHeader("Accept", "application/json");
                if (header != null) {
                    for (String headKey : header.keySet()) {
                        method.setHeader(headKey, header.get(headKey));
                    }
                }
                method.setEntity(
                        new StringEntity(jsonParam, Charset.forName("UTF-8")));


                HttpResponse response = httpClient.execute(method);

                // Read the response body
                body = EntityUtils.toString(response.getEntity());
                //乱码问题
                //	body = new String(body.getBytes("ISO8859-1"), "UTF-8");
            } catch (IOException e) {
                log.error("Https postWithJson 请求异常！", e);
            }
        }
        return body;
    }

}
